package com.iam.util;

import com.alibaba.fastjson.JSON;

/**
 * 为返回前端的数据提供统一接口，Restful规范，Facade模式
 */
public class CommonResult {
    Boolean status; // 处理过程中是否发生报错
    String errorMessage;
    Object result;


    public static CommonResult success(Object result) {
        CommonResult commonResult = new CommonResult();
        commonResult.status = true;
        commonResult.result = result;
        return commonResult;
    }

    public static CommonResult failure(String errorMessage) {
        CommonResult commonResult = new CommonResult();
        commonResult.status = false;
        commonResult.errorMessage = errorMessage;
        return commonResult;
    }

    // 不需要get，set方法吗？是的，时候用new构造的
    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    // Jackson默认会使用反射，所以要有这些方法
    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }
}
